
dpath = '/Users/landauland/Documents/Research/SabatiniLab/data/manuscriptPreparation/compileData';
figure2resB = load(fullfile(dpath,'figure2resultsB'));
resB = figure2resB.f1res;


% Properties 
colWidCM = [85, 114, 174]/10; % in mm
% Axes Size
axAspectRatio = 10.5/13;
axWidth = colWidCM(2)/7 * [2,3]; % Left two columns are 2x, right is 3x
axHeight = 3.35; %colWidCM(3)/4 * (10.5/13);

% Markup properties (smaller for insets!)
fontSize = 7;
lineWidth = 0.5;
markerSize = 4;


% Get f1 distance
f1distance = [resB(:).distance];
f1distidx = (f1distance > 0); % some weren't calculated
f1distance = f1distance(f1distidx);

% Get f1 dendrite AP Peak Calcium 
f1dpk = [resB(f1distidx).dpk];
minAllowed = 0.001;
f1dpk(f1dpk<minAllowed)=minAllowed; % make sure they are not below 0 which is an artifact of noise

% Get f1 spine AP-peak Calcium 
f1spk = {resB(f1distidx).spk};
f1spkIdx = 1:length(f1spk);
f1spkFullIdx = cell2mat(cellfun(@(idx,N) idx*ones(1,length(N)), num2cell(f1spkIdx), f1spk, 'uni', 0));

% Create list of cell IDs after accounting for structure fields with
% multiple ROIs
f1date = {resB(f1distidx).date};
f1id = {resB(f1distidx).cellid};
f1date = f1date(f1spkFullIdx);
f1id = f1id(f1spkFullIdx);
f1cellID = cellfun(@(date,id) [num2str(date),id],f1date,f1id,'uni',0);
[f1ucid,~,f1cellidx] = unique(f1cellID);

% Get all of them! (some are NaNs)
f1spkFull = cell2mat(f1spk);
f1spkFull(f1spkFull<minAllowed) = minAllowed;
f1spkDistance = f1distance(f1spkFullIdx);
f1dpkSpkCompare = f1dpk(f1spkFullIdx);
f1spk = {resB(f1distidx).spk};



% Get pairs to do same recording comparison
idxPairs = cellfun(@(c) length(c)==2, f1spk, 'uni', 1);
distPairs = f1spkDistance(idxPairs);
spkPairs = f1spk(idxPairs);
pairs12 = cell2mat(cellfun(@(c) c([1,2]), spkPairs, 'uni', 0)');




% --Figure 1 figure supplement 1A

setBackX = [-0.025 -0.038 -0.035 -0.085];
setBackY = [-0.025 -0.038 -0.045 -0.125];
figure(1); clf; hold on;
set(gcf,'units','centimeters','position',[40 30 axWidth(1) axHeight]);
set(gca,'units','centimeters','position',[0.8 0.65 axWidth(1)*0.725 axWidth(1)*0.725]); % Note difference for equal axes!
plot(pairs12(:,1),pairs12(:,2),'linestyle','none','marker','.','markersize',markerSize,'color','k');
plot([0,0.5],[0,0.5],'color','k','linewidth',lineWidth,'linestyle','--');
axis('equal')
line([0 0.5],setBackY(1)*[1,1],'color','k','linewidth',lineWidth);
line(setBackX(1)*[1,1],[0 0.5],'color','k','linewidth',lineWidth);
ticks = 0:0.2:0.4;
for t = ticks
    line(t+[0,0],setBackX([1 2]),'color','k','linewidth',lineWidth);
    text(t,setBackX(3),num2str(t),'fontsize',fontSize,'horizontalalignment','center','verticalAlignment','top');
    line(setBackY([1 2]),t+[0,0],'color','k','linewidth',lineWidth);
    text(setBackY(3),t,num2str(t),'fontsize',fontSize,'horizontalalignment','right','verticalAlignment','Middle');
end
text(0.25, setBackX(4), 'S1 \DeltaCa_{AP} (\DeltaG/R)','fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
text(setBackY(4),0.25,'S2 \DeltaCa_{AP} (\DeltaG/R)','fontsize',fontSize,'HorizontalAlignment','center','verticalAlignment','Bottom','Rotation',90);
xlim([setBackY(2) 0.5]);
ylim([setBackX(2) 0.5]);
text(0,0.475,'Neighbor Spines','Fontsize',fontSize-1,'HorizontalAlignment','Left');
text(0,0.425,'(<6 µm apart)','Fontsize',fontSize,'HorizontalAlignment','Left');
set(gca,'visible','off');



% -- Figure 1 - figure supplement 1B
setBackX = [-0.025 -0.038 -0.035 -0.085];
setBackY = [-0.025 -0.038 -0.045 -0.11];
figure(2); clf; hold on;
set(gcf,'units','centimeters','position',[45 30 axWidth(1) axHeight]);
set(gca,'units','centimeters','position',[0.8 0.65 axWidth(1)*0.725 axWidth(1)*0.725]); % Note difference for equal axes!
plot(f1dpkSpkCompare,f1spkFull,'linestyle','none','marker','.','markersize',markerSize,'color','k');
plot([0,0.5],[0,0.5],'color','k','linewidth',lineWidth,'linestyle','--');
axis('equal')
line([0 0.5],setBackY(1)*[1,1],'color','k','linewidth',lineWidth);
line(setBackX(1)*[1,1],[0 0.5],'color','k','linewidth',lineWidth);
ticks = 0:0.2:0.4;
for t = ticks
    line(t+[0,0],setBackX([1 2]),'color','k','linewidth',lineWidth);
    text(t,setBackX(3),num2str(t),'fontsize',fontSize,'horizontalalignment','center','verticalAlignment','top');
    line(setBackY([1 2]),t+[0,0],'color','k','linewidth',lineWidth);
    text(setBackY(3),t,num2str(t),'fontsize',fontSize,'horizontalalignment','right','verticalAlignment','Middle');
end
text(0.25,setBackX(4),'Spine \DeltaCa_{AP}','fontsize',fontSize,'HorizontalAlignment','center','verticalAlignment','top');
text(setBackY(4),0.25,'Parent Dendrite \DeltaCa_{AP}','fontsize',fontSize,'HorizontalAlignment','center','verticalAlignment','bottom','Rotation',90);
xlim([setBackY(2) 0.5]);
ylim([setBackX(2) 0.5]);
set(gca,'visible','off');


